# -*- coding:utf-8 -*-

import datetime

class Volume(object):

    def __init__(self,rt,ti,di,vm,he,vt):
        self.rec_time=rt
        self.tollgate_id=ti
        self.direction=di
        self.vehicle_model=vm
        self.has_etc=he
        self.vehicle_type=vt

    def print_info(self):
        print 'rectime=',self.rec_time
        print ('tollgate_id=%d direction=%d vehicle_model=%d has_etc=%d vehicle_type=%d'
        %(self.tollgate_id,self.direction,self.vehicle_model,self.has_etc,self.vehicle_type))
        
class Volumes(object):

    def __init__(self):
        self.volumes=[]

    def add_volume(self,vol):
        if isinstance(vol,Volume):
            self.volumes.append(vol)
        else:
            print 'not Volume class'

    def print_info(self):
        for i in self.volumes:
            i.print_info()

    def available_dates(self):
        res=[]
        for i in self.volumes:
            if not i.rec_time.date() in res:
                res.append(i.rec_time.date())
        return res
        
    def get(self, toll, dire, day):
        '''
        day是datetime.date类的变量，toll，dire指定了tollgate_id，direction，-1代表不限制
        返回结果是res的list，res[i]存储当天的[i*20,(i+1)*20)时间段内的volumes
        '''
        if not isinstance(day,datetime.date):
            return None
        res=[]
        for i in range(0,72):
            res.append([])
        for i in self.volumes:
            if (i.tollgate_id==toll or toll==-1) and (i.direction==dire or dire==-1):
                if i.rec_time.date() == day:
                    res[i.rec_time.hour*3+i.rec_time.minute/20].append(i)
        return res
    def getByHour(self, dates, toll, dire, seg1, seg2):
        '''
        seg 是时间段的序号，得到每一天[seg1, seg2)时间段的车数组。
        '''
        res = {}
        for i in dates:
            res[i] = []
        for i in self.volumes:
            if (i.tollgate_id==toll or toll==-1) and (i.direction==dire or dire==-1):
                tmp = i.rec_time.hour*3+i.rec_time.minute/20
                if(seg1 <= tmp and tmp < seg2 and i.rec_time.date() in dates):
                    res[i.rec_time.date()].append(i)
        return [res[k] for k in sorted(res.keys())]

def Volumes_test():
    a=Volume(datetime.datetime(2015, 12, 16, 15, 6, 37, 420000),123,456,789,11,-1)
    b=Volumes()
    b.add_volume(a)
    b.print_info()

